//
//  SEGBillPaymentHeaderView.m
//  SEGUhomePropertyBaseService
//
//  Created by Clavis on 2019/3/6.
//  Copyright © 2019 刘山. All rights reserved.
//

#import "SEGBillPaymentHeaderView.h"

@interface SEGBillPaymentHeaderView ()

@property (nonatomic, strong) UIButton *radioButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *feeLabel;

@end

@implementation SEGBillPaymentHeaderView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = kColor_W;
    
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = kColor_W;
    [self addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.radioButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(radioButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select_nor")] forState:UIControlStateNormal];
        [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select")] forState:UIControlStateSelected];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        [self addSubview:button];
        button;
    });
    [self.radioButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(37);
    }];
    
    self.titleLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self addSubview:lable];
        lable.textColor = kColor_1;
        lable.font = [UIFont boldSystemFontOfSize:15];
        lable;
    });
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(37);
        make.centerY.mas_equalTo(0);
    }];
    
    self.feeLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self addSubview:lable];
        lable.textColor = kColor_1;
        lable.font = [UIFont boldSystemFontOfSize:15];
        lable;
    });
    [self.feeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.offset(0);
    }];

    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = kColor_L2;
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
}

- (void)updateUIWithModel:(SEGBillMonthModel *)model isHaveRadio:(BOOL)isHaveRadio {
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(isHaveRadio ? 37 : 15);
        make.centerY.mas_equalTo(0);
    }];
    
    self.radioButton.hidden = !isHaveRadio;
    self.radioButton.selected = model.isSelect;
    self.titleLabel.text = [NSString stringWithFormat:@"%ld年%ld月", (long)model.billYear, (long)model.billMonth];
    self.feeLabel.text = [NSString stringWithFormat:@"￥%.2f", [model.cycleAmount doubleValue] *MoneyRateChange];
}

- (void)radioButtonTap:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (self.changeRadioBlock) {
        self.changeRadioBlock(sender.selected);
    }
}

@end
